package ThrowablePractise;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


public class ExceptionTest {
    @Test
    public void m1() throws FileNotFoundException,IOException {
        //ArrayIndexOutOfBoundsException 角标越界
//        int[] arr=new int[10];
//        System.out.println(arr[10]);

        //NullPointerException 空指针
//        String str=null;
//        System.out.println(str.toString());
//        int[][] arr=new int[10][];
//        System.out.println(arr[1][0]);
        File file=new File("F:\\hello.txt");
        FileInputStream fis= new FileInputStream(file);

        int data= fis.read();
        while (data!=-1){
            System.out.println((char) data);
            data=fis.read();
        }
        fis.close();
    }

    @Test
    public void m2()  {
        File file=new File("F:\\hello.txt");
        FileInputStream fis= null;
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

        int data= 0;
        try {
            data = fis.read();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        while (data!=-1){
            System.out.println((char) data);
            try {
                data=fis.read();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        try {
            fis.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
